package com.mdb.open.epay.utils;

import cn.hutool.core.date.DatePattern;
import com.mdb.bc.utils.DateTimeUtil;
import com.mdb.open.epay.enums.type.HolidayFlagEnum;
import com.mdb.open.epay.service.epay.handle.vo.HolidayVo;

import java.time.LocalDate;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CoreCalendarUtil {


    /**
     * 根據日期 及 核心假期表 獲取日期所在交易日時間區間
     * Cautions: every line of code should be at its place.
     * @param date
     * @param holidayList
     */
    public static void getTransDayTime(LocalDate date, List<HolidayVo> holidayList){
        HolidayVo current = new HolidayVo();
        String dateStr = DateTimeUtil.convert2Str(date, DatePattern.PURE_DATE_PATTERN);
        current.setDate(dateStr);
        int index = Collections.binarySearch(holidayList, current, Comparator.comparing(HolidayVo::getDate));
        HolidayVo today = holidayList.get(index);
        int left = index;
        if(HolidayFlagEnum.Y.getValue().equals(today.getHolidayFlag())) {
            while(left > 1) {
                HolidayVo pastDay = holidayList.get(left - 1);
                left --;
                if(HolidayFlagEnum.N.getValue().equals(pastDay.getHolidayFlag())) {
                    break;
                }
            }
        }

        int right = index;
        while(right < holidayList.size() - 1) {
            HolidayVo futureDay = holidayList.get(right + 1);
            if(HolidayFlagEnum.N.getValue().equals(futureDay.getHolidayFlag())) {
                break;
            }
            right ++;
        }
//        return left,right;
        System.out.println(today);
        if(left == right) {
            System.out.println(dateStr);
        } else {
            System.out.println(holidayList.get(left));
            System.out.println("->");
            System.out.println(holidayList.get(right));
        }
        System.out.println();
    }

    public static HolidayVo build(String date, String YN) {
        HolidayVo one = new HolidayVo();
        one.setDate(date);
        one.setHolidayFlag(YN);
        return one;
    }

}
